import unittest, requests

from ddt import ddt,file_data

@ddt
class A3mall(unittest.TestCase):

    # 1、登录
    token = None
    def test_A3mall_01(self):
        login = requests.post(
            url='http://10.59.9.14:7003/api/public/login',
            json={"username": "15523615759", "password": "123456Aa"}
        )
        print(login.json())
        A3mall.token = login.json()['data']['token']
        # 预期结果与实际结果相等
        self.assertEqual("ok", login.json()['info'])

    # 2、搜索
    spid = None
    def test_A3mall_02(self):
        search = requests.get(url='http://10.59.9.14:7003/api/search/list?page=1&keywords=%E7%83%98%E5%B9%B2&type=0&sort=1')
        print(search.json())
        A3mall.spid = search.json()['data']['list'][0]['id']
        print(A3mall.spid)
        # 预期结果正确
        self.assertTrue("ok" == search.json()['info'])

    # 3、商品详情
    def test_A3mall_03(self):
        view = requests.get(
            url='http://10.59.9.14:7003/api/goods/view?id='+str(A3mall.spid),
            headers = {'Auth-Token': 'Bearer ' + A3mall.token}
        )
        print(view.json())
        # 预期结果不正确
        self.assertFalse("no" == view.json()['info'])

    # 4、加入购物车
    def test_A3mall_04(self):
        add = requests.post(
            url='http://10.59.9.14:7003/api/cart/add',
            headers={'Auth-Token': 'Bearer ' + A3mall.token},
            json={"id":A3mall.spid,"sku_id":"","num":1}
        )
        print(add.json())
        # 预期结果在实际结果里面
        self.assertIn("成功", add.json()['info'])

    # 5、购物车列表
    goodsid = None
    def test_A3mall_05(self):
        cart = requests.get(
            url='http://10.59.9.14:7003/api/cart?page=1',
            headers={'Auth-Token': 'Bearer ' + A3mall.token}
        )
        A3mall.goodsid = cart.json()['data']['list'][0]['id']
        print(cart.json())
        # 预期结果不在实际结果里面
        self.assertNotIn("t", cart.json()['info'])

    # 6、创建订单
    def test_A3mall_06(self):
        confirm = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id='+str(A3mall.goodsid)+'&type=cart&shipping_type=1',
            headers={'Auth-Token': 'Bearer ' + A3mall.token},
        )
        print(confirm.json())
        # 预期结果与实际结果相等
        self.assertEqual("ok", confirm.json()['info'])

    # 7、提交订单
    @file_data('address.json')
    def test_A3mall_07(self, address):
        create = requests.post(
            url='http://10.59.9.14:7003/api/order/create',
            headers={'Auth-Token': 'Bearer ' + A3mall.token},
            json = {"id":A3mall.goodsid,"type":"cart","address_id":address,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":f"http://10.59.9.14:7003/wap/pages/cart/confirm?id={A3mall.goodsid}&type=cart"}
        )
        print(create.json())

if __name__ == '__main__':
    unittest.main()